﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartRS.com.vn.smartrs.util;
using SmartRS.com.vn.smartrs.domain;
using SmartRS.com.vn.smartrs.form.uC;
using SmartRS.com.vn.smartrs.service;

namespace SmartRS.com.vn.smartrs.form.menu
{
    public partial class frmJoinDesk : Form
    {
        static int size { get; set; }
        public static List<Desk> listDesk { get; private set; }
        Desk firstDesk = new Desk();
        List<Desk> lstChooseDesk = new List<Desk>();

        public frmJoinDesk()
        {
            InitializeComponent();
        }

        public void assignData(Desk firstDesk)
        {
            this.firstDesk = firstDesk;
        }

        private void frmJoinDesk_Load(object sender, EventArgs e)
        {
            lblSrcDesk.Text = firstDesk.deskName;
            size = 0;
            initDisable();
            
            // set source bill
            Desk desk = DeskService.getInstance().getById(firstDesk.deskId);
            if (desk == null) { return; }
            lblSrcDesk.Text = "Sảnh " + desk.hall.hallName + " - Bàn " + desk.deskName;

            //set data source for cbo Hall
        }

        private void initDisable()
        {
            ucHallDesk1.Visible = false;
            ucHallDesk2.Visible = false;
            ucHallDesk3.Visible = false;
            ucHallDesk4.Visible = false;
            ucHallDesk5.Visible = false;

            btnSub1.Visible = false;
            btnSub2.Visible = false;
            btnSub3.Visible = false;
            btnSub4.Visible = false;
            btnSub5.Visible = false;

            btnJoin.Location = new Point(btnJoin.Location.X, ucHallDesk0.Location.Y + 40);
            btnCancel.Location = new Point(btnCancel.Location.X, ucHallDesk0.Location.Y + 40);
            this.Height = btnAdd.Location.Y + 140;

            //ucHallDesk0.setValue(getCurrentListDeskHaveBill().ToList());
        }

        private IList<Hall> getCurrentListDeskHaveBill()
        {
            IList<Hall> listHall = HallService.getInstance().getAll();
            if (listHall == null || listHall.Count <= 0) { return null; }
            IList<Desk> lstDeskTemp = new List<Desk>();

            foreach (Hall hall in listHall)
            {
                if (hall.deskList == null || hall.deskList.Count <= 0) { continue; }
                lstDeskTemp = new List<Desk>();
                //Get list desk have current bill
                foreach (Desk desk in hall.deskList)
                {
                    if (desk.billList == null || desk.billList.Count <= 0) { continue; }
                    //Continue if exist in current choose list desk
                    bool isExist = false;
                    foreach (Desk item in lstChooseDesk)
                    {
                        if (desk.deskId == item.deskId) { isExist = true; break; }
                    }
                    if (isExist) { continue; }

                    //Find desk have current bill not exist in choose desk
                    if (desk.status == tableState.using_)
                    {
                        foreach (Bill bill in desk.billList)
                        {
                            if (!bill.release)
                            {
                                lstDeskTemp.Add(desk);
                                break;
                            }                            
                        }
                    }
                }
                hall.deskList = lstDeskTemp;
            }

            return listHall;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (size == RSConst.MAX_NUMBER_DESK_JOIN) { return; }
            var objUC = new Control() ;
            var objBtn = new Control();
            int i = 1;
            for (i = i; i < 6; i++)
            {
                objUC = this.Controls.Find("ucHallDesk" + i.ToString(), false)[0];
                if (objUC.Visible == false)
                {
                    objBtn = this.Controls.Find("btnSub" + i.ToString(), false)[0];
                    objBtn.Location = new Point(objBtn.Location.X, ucHallDesk0.Location.Y + 40 * i);
                    objUC.Location = new Point(objUC.Location.X, ucHallDesk0.Location.Y + 40 * i);

                    objBtn.Visible = true;
                    objUC.Visible = true;
                    break;
                }
            }
            for (i = i + 1; i <= RSConst.MAX_NUMBER_DESK_JOIN; i++)
            {
                objUC = this.Controls.Find("ucHallDesk" + i.ToString(), false)[0];
                objBtn = this.Controls.Find("btnSub" + i.ToString(), false)[0];
                if (objUC.Visible == true)
                {
                    objBtn.Location = new Point(objBtn.Location.X, objBtn.Location.Y + 40);
                    objUC.Location = new Point(objUC.Location.X, objUC.Location.Y + 40);
                }
            }
            

            //resize windown
            this.Height = this.Height + ucHallDesk0.Height + 10;

            btnJoin.Location = new Point(btnJoin.Location.X, btnJoin.Location.Y + ucHallDesk0.Height + 10);
            btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + ucHallDesk0.Height + 10);

            size++;
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            int btnId = int.Parse(btn.Name.Replace("btnSub", ""));
            switch (btnId)
            {
                case 1:
                    btnSub1.Visible = false;
                    ucHallDesk1.Visible = false;

                    btnSub5.Location = new Point(btnSub4.Location.X, btnSub4.Location.Y);
                    ucHallDesk5.Location = new Point(ucHallDesk4.Location.X, ucHallDesk4.Location.Y);
                    btnSub4.Location = new Point(btnSub3.Location.X, btnSub3.Location.Y);
                    ucHallDesk4.Location = new Point(ucHallDesk3.Location.X, ucHallDesk3.Location.Y);
                    btnSub3.Location = new Point(btnSub2.Location.X, btnSub2.Location.Y);
                    ucHallDesk3.Location = new Point(ucHallDesk2.Location.X, ucHallDesk2.Location.Y);
                    btnSub2.Location = new Point(btnSub1.Location.X, btnSub1.Location.Y);
                    ucHallDesk2.Location = new Point(ucHallDesk1.Location.X, ucHallDesk1.Location.Y);
                    break;
                case 2:
                    btnSub2.Visible = false;
                    ucHallDesk2.Visible = false;
                    
                    btnSub5.Location = new Point(btnSub4.Location.X, btnSub4.Location.Y);
                    ucHallDesk5.Location = new Point(ucHallDesk4.Location.X, ucHallDesk4.Location.Y);
                    btnSub4.Location = new Point(btnSub3.Location.X, btnSub3.Location.Y);
                    ucHallDesk4.Location = new Point(ucHallDesk3.Location.X, ucHallDesk3.Location.Y);
                    btnSub3.Location = new Point(btnSub2.Location.X, btnSub2.Location.Y);
                    ucHallDesk3.Location = new Point(ucHallDesk2.Location.X, ucHallDesk2.Location.Y);
                                        
                    break;
                case 3:
                    btnSub3.Visible = false;
                    ucHallDesk3.Visible = false;

                    btnSub5.Location = new Point(btnSub4.Location.X, btnSub4.Location.Y);
                    ucHallDesk5.Location = new Point(ucHallDesk4.Location.X, ucHallDesk4.Location.Y);
                    btnSub4.Location = new Point(btnSub3.Location.X, btnSub3.Location.Y);
                    ucHallDesk4.Location = new Point(ucHallDesk3.Location.X, ucHallDesk3.Location.Y);
                    break;
                case 4:
                    btnSub4.Visible = false;
                    ucHallDesk4.Visible = false;

                    btnSub5.Location = new Point(btnSub4.Location.X, btnSub4.Location.Y);
                    ucHallDesk5.Location = new Point(ucHallDesk4.Location.X, ucHallDesk4.Location.Y);
                    break;
                case 5:
                    btnSub5.Visible = false;
                    ucHallDesk5.Visible = false;
                    break;
            }


            this.Height = this.Height - ucHallDesk0.Height - 10;

            btnJoin.Location = new Point(btnJoin.Location.X, btnJoin.Location.Y - ucHallDesk0.Height - 10);
            btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y - ucHallDesk0.Height - 10);

            size--;

        }

        private void ucHallDesk1_VisibleChanged(object sender, EventArgs e)
        {

        }

    }
}
